<?php
  class DataConverter{
      /**
      * 将数据格式化显示
      * 
      * @param mixed $bits
      * @return numeric
      */
      public static function format($bits){
          $mb = 1048576; 
          if($mb <= $bits){
              $bitStr = number_format( round( $bits / $mb, 2 ), 2 )." MB"; 
              return $bitStr;
          }
          if(1024 <= $bits){
              $bitStr = number_format( round( $bits / 1024, 2 ), 2 )." KB"; 
              return $bitStr;
          }
          $bitStr = number_format( round( $bits, 2 ), 2 )." B";
          return $bitStr;
      }
      
      /**
      * 截取字符串，并显示成  文字...
      * 
      * @param mixed $string
      * @param mixed $start
      * @param mixed $length
      * @return string
      */
      public static function mbstring($string, $start, $length){
          if(strlen($string)>$length){
              $str=null;
              $str=mb_substr($string,$start,$length,"utf-8");
              return $str.'...';
          }else{
              return $string;
          }
      }
      
      /**
      * 生成摘要
      * 
      * @param mixed $text
      * @param mixed $length
      * @return string
      */
      public static function getAbstract($document,$length=600){
          $document = trim($document);
          if(strlen($document) <= $length){
              return $document;
          }
          $result = strip_tags($document);
          return self::mbstring($result,0,$length);
      }
          
      /**
      * 判断大小是否符合附件规定的尺寸  
      * 
      * @param mixed $value
      * @return boolean
      */
      public static function isAttachSize($value){
          if(is_numeric($value)){
              if(($value>=1)&&($value<=20480)){
                  return true;
              }
              return false;
          }
          return false;
      }
      
      /**
      * 判断变量是否为email
      * 
      * @param mixed $url
      * @return boolean
      */
      public static function isURL($url){
          if(empty($url)){
              return false;
          }
          return preg_match("/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"])*$/", $url);
      }
      
      /**
      * 判断变量是否为email
      * 
      * @param mixed $email
      * @return boolean
      */
      public static function isEmail($email){
          if(empty($email)){
              return false;
          }
          return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-]+(\.\w+)+$/", $email);
      }
      
      /**
      * 检查是否是XML文件
      * 
      * @param mixed $xml
      * @return boolean
      */
      public static function isXML($xml){  
          if(is_file($xml)){
              $file_ext = pathinfo($xml,PATHINFO_EXTENSION);
              if($file_ext == 'xml'){
                  return true;
              }               
              return false; 
          }
          else{
              return false;
          }
      }
      
      /**
      * 判断变量值是否为IP
      * 
      * @param array $ip
      * @return boolean
      */
      public static function isIP($ip){
          $temp = explode(".",$ip);
          for($i=0;$i<count($temp);$i++){
              if($temp[$i]>255){
                  return (0);
              }
          }
          return ereg("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$",$ip); 
      }
      
      /**
      * 根据IP获取物理地址
      * 
      * @param mixed $ip
      */
      public static function getIpPlace($ip){                                    
          $url = 'http://int.dpool.sina.com.cn/iplookup/?ip='.$ip;
          $ch = curl_init();
          curl_setopt($ch,CURLOPT_URL,$url);
          curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
          $output = curl_exec($ch);
          curl_close($ch);
          $output=explode("\t",$output);
          $result = $output[4].$output[5].$output[6].'('.$output[7].')'.$output[9]; 
          return iconv('gbk','utf-8',$result);
      }
      
      /**
      * 初始化序列号
      * 
      */
      public static function initSerialNo($table){
          global $db;
          $sql = "SHOW COLUMNS FROM $table";
          $columns = $db->query($sql);
          $sql = "select max(".$columns[0][0].") from $table";
          $maxSerialNo = $db->query($sql);
          $maxSerialNo = $maxSerialNo[0][0];
          
          $serialNo = '';                                     
          $time = DateUtil::get_YY_mm_dd_HH_mm_ss();
          $time = str_replace('-','',$time);
          $time = str_replace(':','',$time);
          $time = str_replace(' ','',$time);
          
          if(strlen($maxSerialNo) < 16){
              $serialNo = $time.'01';
          }
          else if(substr($maxSerialNo,0,14) == $time){
              $num = substr($maxSerialNo,14,2);
              $num = intval($num);
              $num++;
              $num = strval($num);
              if(strlen($num) < 2){
                  $num = '0'.$num;
              }
              $serialNo = $time.$num;
          }
          else{
              $serialNo = $time.'01';
          }
          return $serialNo;
      }
      
      /**
      * 转换日期,YYYY-MM-DD 转换为 年月日
      * 
      * @param mixed $date
      */
      public static function getChineseDate($date){
          $len = strlen($date);
          if($len > 10) return '';
          if($len == 10){
              $year = intval(substr($date,0,4));
              $month = intval(substr($date,5,2));
              $day = intval(substr($date,8,2));
              return $year.'年'.$month.'月'.$day.'日';
          }
          else if($len == 7){
              $year = intval(substr($date,0,4));
              $month = intval(substr($date,5,2));
              return $year.'年'.$month.'月';
          }
          else{
              return '';
          }
      } 
      
      /**
      * 接收参数(单个)，并过滤不良信息
      * 
      * @param mixed $param
      * @param mixed $pi_iType
      * @return mixed
      */
      public static function getParam($param,$isNumeric='n'){
          if(isset($_GET[$param]))
              $value = trim($_GET[$param]);
          else if ( isset($_POST[$param]))
              $value = trim($_POST[$param]);
          else
              return null;
          
          if($isNumeric == 'y'){
              if(is_numeric($param)){
                  return $param;
              }
              return null;
          }
          
          if(!get_magic_quotes_gpc()){
              $value = addslashes($value);
          }
          return $value;
      }
  }
?>
